package com.fin.zw.intelligentbot.controller;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.entity.Company;
import com.fin.zw.intelligentbot.model.request.CompanyRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.CompanyService;
import com.querydsl.core.types.Predicate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/company")
@Api(value = "企业信息管理",description = "企业信息管理")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/fetchPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<Company>> queryCompanyAll(Pageable pageable,
                                                         @QuerydslPredicate(root = Company.class) Predicate predicate){
        Page<Company> page = companyService.queryCompanyList(pageable,predicate);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "获取企业信息",notes = "获取企业信息")
    public ResponseEntity<List<Company>> getCompanyList(@ApiIgnore @CurrentUser SessionModel sessionModel){
        List<Company> list = companyService.getCompanyList(sessionModel);
        return ResponseEntity.ok().body(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "企业信息添加",notes = "企业信息添加")
    public ResponseEntity<String> createCompany(@RequestBody CompanyRequest request,
                                              @ApiIgnore @CurrentUser SessionModel sessionModel ){
        companyService.createCompany(request,sessionModel);
        return ResponseEntity.ok().body("添加成功");
    }

    @RequestMapping(value = "/update",method = {RequestMethod.PUT,RequestMethod.PATCH})
    @ApiOperation(value = "企业信息添加",notes = "企业信息添加")
    public ResponseEntity<String> updateCompany(@RequestBody CompanyRequest request){
        companyService.updateCompany(request);
        return ResponseEntity.ok().body("修改成功");
    }

}
